// pages/equipment/error.js
const app = getApp()
const utils = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    skin: '',
    oxtId : 0,
    typeId : 0,
    areaId : 0,
    haveNext: false,
    normalSum: 0,
    errorSum: 0,
    dataList: []
  },

  toPageNormal() {
    wx.redirectTo({
      url: '/pages/equipment/normal',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      typeId: options.typeId || 0,
      oxtId : options.oxtId || 0,
      areaId : options.areaId || 0,
      page : 1
    });
    this.loadList(1);

    app.setNavBar()
    const { skin } = app.globalData
    this.setData({
      skin
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.haveNext) {
      this.setData({
        page: this.data.page + 1
      });
      this.loadList(this.data.page);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  loadList: function (page) {
    let that = this;
    let getDeviceListParams = {
      "typeId": that.data.typeId,
      "oxtallId" : that.data.oxtId,
      "areaId" : that.data.areaId,
      "powerStatus" : 1,
      "page": page,
      "pageSize": 10,
    };
    utils.promisePost('/wx/property/getDeviceList', getDeviceListParams).then(res => {
      let deviceList = that.data.dataList;
      let haveNext = that.data.haveNext;
      if (page == 1) {
        deviceList = [];
      }
      if (res.code == 200) {
        let result = res.data;
        if (result.rows.length > 0) {
          for (let i = 0; i < result.rows.length; i++) {
            let itemRow = {
              "no" : result.rows[i].dceNo,
              "barnName" : result.rows[i].oxtName,
              "zoneName" : result.rows[i].oxtAreaName,
              "areaId" : result.rows[i].oxtAreaId,
              "oxtId" : result.rows[i].oxtId,
              "typeId" : result.rows[i].typeId,
              "errorSum" : 10
            };
            deviceList.push(itemRow);
          }
          haveNext = true;
        } else {
          haveNext = false;
        }
        console.log(deviceList);
      } else {
      }
      this.setData({
        dataList: deviceList,
        haveNext: haveNext
      });
    }).catch(e => {
      console.log(e);
    });
  }
})